package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.lifecycle.domain.LifManageList;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.SysMarketMapper;
import com.ruoyi.system.service.IRelatedTodoeventService;
import com.ruoyi.system.service.ISysMarketService;
import com.ruoyi.system.service.ISysTodoeventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SysMarketServiceImpl implements ISysMarketService {

    @Autowired
    private SysMarketMapper sysMarketMapper;

    @Autowired
    private ISysTodoeventService sysTodoeventService;

    @Autowired
    private IRelatedTodoeventService relatedTodoeventService;

    @Override
    public List<SysMarket> selectSysMarketListById(Long deviceId) {
        return sysMarketMapper.selectSysMarketListById(deviceId);
    }

    @Override
    public int insertSysMarket(SysMarket sysMarket) {

        Date date = new Date();
        sysMarket.setChangeTime(date);
        SysTodoevent sysTodoevent = new SysTodoevent();
        sysTodoevent.setPublishId(SecurityUtils.getUserId());
        sysTodoevent.setHandleId(sysMarket.getApprover());
        sysTodoevent.setRelatedtaskId(sysMarket.getSysDeviceId());
        sysTodoevent.setLifStatus(1);
        sysTodoevent.setProjectId(sysMarket.getSysId());
        sysTodoevent.setStatus(0L);
        sysTodoevent.setReminder("请您审批"+sysMarket.getDevName()+"市场调研报告");
        sysTodoevent.setRequestUrl("marketResearch");
        sysTodoeventService.insertSysTodoevent(sysTodoevent);
        //向中间表中插入数据
        RelatedTodoevent relatedTodoevent = new RelatedTodoevent();
        relatedTodoevent.setRelatedId(sysMarket.getSysDeviceId());
        relatedTodoevent.setTodoeventId(sysTodoevent.getId());
        relatedTodoeventService.insertRelatedTodoevent(relatedTodoevent);
        return sysMarketMapper.insertSysMarket(sysMarket);
    }

    @Override
    public Long insertPathName(SysMarketFile sysMarket) {


        Date date = new Date();
        sysMarket.setUpTime(date);
        sysMarketMapper.insertPathName(sysMarket);
        return sysMarket.getFileId();
    }

    @Override
    public int updateSysMarket(SysMarket sysMarket) {
        Date date = new Date();
        sysMarket.setChangeTime(date);
        return sysMarketMapper.updateSysMarket(sysMarket);
    }

    @Override
    public int deleteMarketFileById(Long fileId) {
        return sysMarketMapper.deleteMarketFileById(fileId);
    }

    @Override
    public List<SysMarketFile> selectDocFileByFileId(Long id) {
        return sysMarketMapper.selectDocFileByFileId(id);
    }

    @Override
    public SysMarket selectMarketById(Long id) {
        return sysMarketMapper.selectMarketById(id);
    }


}
